/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.ShipperProcessor;

import com.sun.Entity.Invoicefordelaypenalty;
import com.sun.Entity.Shippingorder;
import com.sun.Facade.InvoicefordelaypenaltyFacadeLocal;
import com.sun.Facade.ShippingorderFacadeLocal;
import exceptions.PenalityAmountNegativeException;
import exceptions.UnknownOrderException;
import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ejb.Stateless;

/**
 *
 * @author test
 */
@WebService()
@Stateless()
public class CreatePenalityInvoiceWS {

    @EJB
    private InvoicefordelaypenaltyFacadeLocal invoicefordelaypenaltyFacade;
    @EJB
    private ShippingorderFacadeLocal shippingorderFacade;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "CreatePenalityInvoice")
    public String CreatePenalityInvoice(
            @WebParam(name = "OrderID") String OrderID,
            @WebParam(name = "PenalityAmount") Float PenalityAmount)
            throws
            UnknownOrderException,
            PenalityAmountNegativeException {

        // Verification OrderID existance
        Shippingorder Order = shippingorderFacade.find(OrderID);
        if (Order == null) {
            throw new UnknownOrderException();
        }

        if (PenalityAmount <= 0) {
            throw new PenalityAmountNegativeException();
        }

        Invoicefordelaypenalty inv = new Invoicefordelaypenalty();
        inv.setOrderid(Order);
        inv.setPinvoiceid(OrderID);
        inv.setAmount(PenalityAmount);
        invoicefordelaypenaltyFacade.create(inv);

        return OrderID;
    }
}
